package com.cxt.service.impl;//package com.baomidou.service.impl;

//import com.baomidou.entity.MessageNotification;
//import com.baomidou.mapper.MessageNotificationMapper;
//import com.baomidou.service.IMessageNotificationService;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cxt.Mapper.MessageNotificationMapper;
import com.cxt.context.BaseContext;
import com.cxt.entity.*;
import com.cxt.service.*;
import com.cxt.vo.SosJSonVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

//import static com.sun.tools.javac.resources.CompilerProperties.Fragments.Lambda;

/**
 * <p>
 * 消息通知表 服务实现类
 * </p>
 *
 * @author baomidou
 * @since 2025-02-27
 */
@Service
public class MessageNotificationServiceImpl extends ServiceImpl<MessageNotificationMapper, MessageNotification> implements IMessageNotificationService {

    @Autowired
    private IUserService userService;
    @Autowired
    private ITripInfoService  tripInfoService;
    @Autowired
    private StudentInfoService studentInfoService;
    @Autowired
    private DriverInfoService driverInfoService;
    @Override
    public Boolean addMassage(Long userId, String msg) {
        MessageNotification messageNotification = MessageNotification.builder()
                .userId(userId)
                .msg(msg)
                .isRead(false)
                .createTime(LocalDateTime.now())
                .build();
        return save(messageNotification);
    }

    @Override
    public List<MessageNotification> getMyMsg() {
        //获取该用户的未读消息
        LambdaQueryWrapper<MessageNotification> query = new LambdaQueryWrapper<>();
        query.eq(MessageNotification::getUserId, BaseContext.getCurrentId());
        query.eq(MessageNotification::getIsRead, false);
        //时间倒叙
        query.orderByDesc(MessageNotification::getCreateTime);
        List<MessageNotification> list = this.list(query);
//        list.forEach(item -> {
//            item.setIsRead(true);
//            updateById(item);
//        });
        //该用户已读
        LambdaQueryWrapper<MessageNotification> query1 = new LambdaQueryWrapper<>();
        query1.eq(MessageNotification::getUserId, BaseContext.getCurrentId());
        query1.eq(MessageNotification::getIsRead, true);
        query1.orderByDesc(MessageNotification::getCreateTime);
        List<MessageNotification> list1 = this.list(query1);
        list.addAll(list1);
        //过滤掉msg第一个字符是{的
        list.removeIf(item -> item.getMsg().charAt(0) == '{');
        return list;
    }

    @Override
    @Transactional
    public Boolean helpTripInfo(Long id) {

        //查找所有管理员用户
        LambdaQueryWrapper<User> query = new LambdaQueryWrapper<>();
        query.eq(User::getIsAdmin, true);
        List<User> list = userService.list(query);
        //获取司机user和司机认证
        TripInfo tripInfo = tripInfoService.getById(id);
        User driverUser = userService.getById(tripInfo.getDriverId());
        //
        LambdaQueryWrapper<DriverInfo> query1 = new LambdaQueryWrapper<>();
        query1.eq(DriverInfo::getUserId, tripInfo.getDriverId());
        DriverInfo driverInfo = driverInfoService.getOne(query1);
        //获取学生信息和学生认证
        User sosUser = userService.getById(BaseContext.getCurrentId());
        //
        LambdaQueryWrapper<StudentInfo> query2 = new LambdaQueryWrapper<>();
        query2.eq(StudentInfo::getUserId, BaseContext.getCurrentId());
        StudentInfo studentInfo = studentInfoService.getOne(query2);
        SosJSonVo sosJSonVo  = SosJSonVo.builder()
                .driverUser(driverUser)
                .driverInfo(driverInfo)
                .tripInfoVo(tripInfo)
                .sosUser(sosUser)
                .sosStudentInfo(studentInfo)
                .build();
        //转成Json
        String json = JSON.toJSONString(sosJSonVo);
        for (User user : list) {
            //给管理员发送消息
            addMassage(user.getId(), json);
        }
        return true;
    }

    @Override
    public List<MessageNotification> getHelpMsg() {  //获取该用户的未读消息
        LambdaQueryWrapper<MessageNotification> query = new LambdaQueryWrapper<>();
        query.eq(MessageNotification::getUserId, BaseContext.getCurrentId());
        query.eq(MessageNotification::getIsRead, false);
        //时间倒叙
        query.orderByDesc(MessageNotification::getCreateTime);
        List<MessageNotification> list = this.list(query);
//        list.forEach(item -> {
//            item.setIsRead(true);
//            updateById(item);
//        });
        //该用户已读
        LambdaQueryWrapper<MessageNotification> query1 = new LambdaQueryWrapper<>();
        query1.eq(MessageNotification::getUserId, BaseContext.getCurrentId());
        query1.eq(MessageNotification::getIsRead, true);
        query1.orderByDesc(MessageNotification::getCreateTime);
        List<MessageNotification> list1 = this.list(query1);
        list.addAll(list1);
        //过滤掉msg第一个字符是{的
        list.removeIf(item -> item.getMsg().charAt(0) != '{');
        return list;
    }
}
